package com.hushao.lims.weblims.controller;

import com.hushao.lims.weblims.domain.ClassInfo;
import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.service.ClassService;
import com.hushao.lims.weblims.service.TeacherService;
import com.hushao.lims.weblims.utils.UserUtil;
import com.hushao.lims.weblims.vo.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 930588706
 * @date 18/2/1
 * 班级
 */
@Controller
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private ClassService classService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private UserUtil userUtil;

    /**
     * 根据班级名称模糊查询班级列表（分页）
     * @param className
     * @param async
     * @param pageable
     * @param model
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "模糊查询班级列表（分页）")
    @PreAuthorize("hasAnyAuthority('ROLE_主管','ROLE_管理员','ROLE_超级管理员')")
    @GetMapping
    public String listClassByName(@RequestParam(value = "className",required = false,defaultValue = "")String className,
                                  @RequestParam(value = "async",required = false) boolean async,
                                  Pageable pageable,
                                  Model model) throws Exception{
        Page<ClassInfo> classInfoPage=classService.listClassByName(className,pageable);
        List<ClassInfo> classInfos=classInfoPage.getContent();
        model.addAttribute("page",classInfoPage);
        model.addAttribute("classs",classInfos);
        TeacherInfo teacher= userUtil.isLogged(SecurityContextHolder.getContext().getAuthentication());
        //获取数据库的角色而不是获取当前静态登陆角色
        TeacherInfo teacher1=teacherService.getById(teacher.getTeacherId());
        //判断是否是超级管理员角色或者管理员角色
        boolean isBo=userUtil.isAdministrator(teacher1)||userUtil.isAdmin(teacher1);
        model.addAttribute("isBo",isBo);
        return async==true?"class/list ::#mainContainerRepleace":"class/list";

    }

    /**
     * 获取添加班级界面
     * @param model
     * @return
     */
    @ApiOperation(value = "获取添加班级界面")
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @GetMapping("/add")
    public String addInterface(Model model){
        ClassInfo classInfo=new ClassInfo(null,null,null);
        model.addAttribute("class",classInfo);
        return "/class/edit";
    }

    /**
     * 获取编辑班级界面
     * @param classId
     * @param model
     * @return
     */
    @ApiOperation(value = "获取编辑班级界面")
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @GetMapping("/edit/{classId}")
    public String editInterface(@PathVariable("classId") Long classId, Model model) {
        ClassInfo classInfo=classService.getOne(classId);
        model.addAttribute("class",classInfo);
        return "/class/edit";
    }

    /**
     * 增加或修改班级
     * @param classInfo
     * @param teacherNumber
     * @throws Exception
     * @return
     */
    @ApiOperation(value = "增加或修改班级服务")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @PostMapping
    public ResponseData saveOrUpdate(ClassInfo classInfo,String teacherNumber)throws Exception{
        String message=classService.saveOrUpdate(classInfo,teacherNumber);
        return new ResponseData(true,message);
    }

    /**
     * 删除班级
     * @param classId
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "删除班级服务")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('ROLE_管理员','ROLE_超级管理员')")
    @DeleteMapping("/{classId}")
    public ResponseData delete(@PathVariable("classId") Long classId)throws Exception {
        classService.removeClass(classId);
        return new ResponseData(true,"删除成功！");
    }

    /**
     * 班级课程
     * @param classId
     * @param model
     * @return
     * @throws Exception
     */
    @ApiOperation(value ="班级课程")
    @GetMapping("/{classId}/timetables")
    public String timetables(@PathVariable("classId") Long classId, Model model)throws Exception{
        ClassInfo classInfo=classService.getOne(classId);
        Map<String, Map<String, String>> map=classService.timetables(classInfo);
        model.addAttribute("map",map);
        return "/class/timetables";
    }
}
